package rabc.veiw;

import rabc.domain.dto.DeptDto;
import rabc.domain.vo.DeptVo;
import rabc.domain.vo.Result;
import rabc.service.Deptservice;

public class  DeptVeiw {
    public static DeptVo[] cache;

    public static  void show() {
        String menuChoice;
        do {
            printMenu();
            menuChoice = MainVeiw.input.nextLine();
            switch (menuChoice) {
                case "0":
                    MainVeiw.show();
                    break;
                case "1":
                    System.out.println("");
                    break;
                case "2":
                    System.out.println("");
                    break;
                case "3":
                    System.out.println("");
                    break;
                case "4":
                    System.out.println("");
                    break;
                default:
                    System.out.println();
            }
        }while(!"0".equals(menuChoice));
        MainVeiw.input.close();
    }
    public static void  printMenu(){

        String Menu= """
                -------------------
                    部门管理
                -------------------
                | 0 返回上级        |
                | 1 部门列表        |
                | 2 创建部门        |
                | 3 更新部门        |
                | 4 删除部门        |
                -------------------
                请输入选项：
                """;
        System.out.println(Menu);
    }
    public static void printError(String message){
        System.out.println("\033[31m"+message+"\033[0m"  );
    }
    public static void list(){
        Result result = Deptservice.list();
        cache=(DeptVo[]) result.data;
        System.out.println("部门编号\t部门名称\t部门电话");
        for (DeptVo deptVo : cache) {
            System.out.printf("%d\t%s\t%s\n", deptVo.id, deptVo.name, deptVo.phone);
        }
    }
    public static void create(){
        System.out.println("请输入部门名称：");
        String name = MainVeiw.input.nextLine();
        System.out.println("请输入部门电话：");
        String phone = MainVeiw.input.nextLine();
        DeptDto dto = new DeptDto(name,phone);
        Result result = Deptservice.create(dto);
        if(!result.success){
            printError(result.message);
            return;
        }else{
            System.out.println(result.message);
            list();
        }
    }
    public static void update(){
        DeptVo dept = getCacheById();
        if (dept==null){
            return;
        }
        System.out.print("请输入部门名称：直接回车表示不修改");
        String name = MainVeiw.input.nextLine();
        name="".equals(name)?dept.name:name;
        System.out.print("请输入部门电话：直接回车表示不修改");
        String phone = MainVeiw.input.nextLine();
        phone="".equals(phone)?dept.phone:phone;
        DeptDto dto = new DeptDto(name,phone);
        Result result = Deptservice.update(dto,dept.id);
        if(!result.success){
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }
    public static void delete(){
        DeptVo dept = getCacheById();
        if (dept==null){
            return;
        }
        System.out.println("确认删除吗？y确认，其他取消/n");
        String confirm = MainVeiw.input.nextLine();

        if ("y".equalsIgnoreCase(confirm)) {
            System.out.println("已删除");
            return;
        }
        Result result = Deptservice.delete(dept.id);
        if(!result.success){
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();

    }
    public static DeptVo getCacheById(){
        System.out.println("请输入id编号：id必须是大于零的整数");
        if (!MainVeiw.input.hasNextInt()){
            MainVeiw.input.nextLine();
            System.out.println("id必须是整数，请输入整数");
            return null;
        }
        int id = MainVeiw.input.nextInt();
        MainVeiw.input.nextLine();
        if (id <= 0){
            System.out.println("id必须是大于零的整数");
            return null;
        }
        if(cache==null){
            Result result = Deptservice.list();
            cache=(DeptVo[]) result.data;
        }
        DeptVo dept =null;
        for (DeptVo item : cache) {
            if (item.id == id) {
                System.out.println("该ID对应的数据是：");
                System.out.printf("id%d\t名称%s\t电话%s\n", item.id, item.name, item.phone);
                dept=item;
                break;
            }
        }
        if (dept==null){
            System.out.println("没有找到对应的数据");
            return null;
        }
        return dept;
    }

}
